package com.py.credit.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.DictDetailEntity;
import com.py.credit.entity.SysUser;
import com.py.credit.entity.XyjgDrqydf;
import com.py.credit.entity.common.OptionalColumn;
import com.py.credit.entity.enums.ResourceSzjgDrqyFieldsEnum;
import com.py.credit.entity.enums.ResourceSzjgDrqydfFieldsEnum;
import com.py.credit.entity.enums.ResourceSzjgXyjgFieldsEnum;
import com.py.credit.entity.enums.ResourceSzjgXyjgqyFieldsEnum;
import com.py.credit.entity.enums.ResourceSzjgXyjgtjFieldsEnum;
import com.py.credit.entity.enums.ResourceSzjgXypjFieldsEnum;
import com.py.credit.service.SysDictService;
import com.py.credit.service.SzjgService;
import com.py.credit.service.XypjRemoteBaseService;
import com.py.credit.utils.ExcelUtil;
import com.py.credit.utils.FileUtils;
import com.py.credit.utils.MapCache;
import com.py.framework.core.common.Page;
import com.py.framework.core.entity.SysOfficeEntity;
import com.py.framework.core.exception.ServiceException;
import com.py.framework.core.helper.HttpServletHelper;

@RestController
@RequestMapping(value = "/szjg")
public class SzjgController {

	private final static String FRONT_TABLE_ID = "szjg";
	
	private final static String ywId = "1";
	
	@Autowired
	private XypjRemoteBaseService xypjRemoteBaseService;
	
	 @Autowired
	    private SysDictService sysDictService;
	
	@Autowired
	private SzjgService szjgService;
	
	@RequestMapping(value = "/getXypjTableList.json")
	public RestServiceResult<List<Map<String, Object>>> getTableList(
			@RequestParam(value = "id", defaultValue = "", required = true) String id,
			@RequestParam(value = "rank", defaultValue = "", required = true) String rank,
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
			HttpServletRequest request) {

		String[] configColumns = (String[]) MapCache.get(1000 + "_" + FRONT_TABLE_ID);
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}
		String[] additionalColumns = { "id" };
		List<String> showColumns = (configColumns == null || configColumns.length == 0)
				? ResourceSzjgXypjFieldsEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);
		RestServiceResult<List<Map<String, Object>>> r = new RestServiceResult<List<Map<String, Object>>>();
		try {
			List<Map<String, Object>> tableList = null;
			int i  = userInfo.getUserType();
			if(i == 3 || i == 5 || i == 2){
				
			}else{
				if(userInfo.getParentId() == 0){
					id = String.valueOf(userInfo.getOfficeId());
				}else{
					id = String.valueOf(userInfo.getParentId());
				}
			}
				Map<String, Object> modelMap = szjgService.getModelByOffice(ywId,id);
				if(modelMap != null && modelMap.get("model_id") != null){
					tableList = szjgService.getByConditionForPage(rank,String.valueOf(modelMap.get("model_id")),showColumns, additionalColumns,
							condition, page);
				}else{
					tableList = szjgService.get2ByConditionForPage(rank,showColumns, additionalColumns,
							condition, page);
				}
			
			r = new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}
	
	
	@RequestMapping(value = "/getBumeng.json")
	public RestServiceResult<List<Map<String, Object>>> getBumeng(HttpServletRequest request) {
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
		try {
			
			String token = HttpServletHelper.getUserToken(request);
			// 获取当前用户信息
			RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
			SysUser userInfo = null;
			if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
				userInfo = user.getData();
				if (null == userInfo || userInfo.getId() <= 0) {
					result.setCode(RestServiceResult.CODE_UNKNOW);
					result.setMsg("系统异常，请稍后再试！");
					ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
					return result;
				}
			} else {
				result.setCode(user.getCode());
				result.setMsg(user.getMsg());
				return result;
			}
			
			int i  = userInfo.getUserType();
			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			if(i == 3 || i == 5 || i == 2){
				List<String> bumengList = szjgService.getBumeng(ywId);
				for(String bumeng: bumengList){
					if(bumeng != null && !"".equals(bumeng)){
						RestServiceResult<SysOfficeEntity> rsysOfficeEntity = xypjRemoteBaseService.getOfficesById(Long.parseLong(bumeng));
						SysOfficeEntity sysOfficeEntity = null;
						if (RestServiceResult.CODE_SUCCESS == rsysOfficeEntity.getCode()) {
							sysOfficeEntity = rsysOfficeEntity.getData();
							if (null != sysOfficeEntity) {
								
								Map<String, Object> map = new HashMap<String, Object>();
								map.put("id", sysOfficeEntity.getId());
								map.put("name", sysOfficeEntity.getOfficeName());
								list.add(map);
							}
						} else {
							result.setCode(user.getCode());
							result.setMsg(user.getMsg());
							return result;
						}
					}
				}
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", "");
				map.put("name", "公共");
				list.add(map);
			}else {
				long officeId = 0;
				String officeName = "";
				if(0 == userInfo.getParentId()){
					 officeId = userInfo.getOfficeId();
					 officeName = userInfo.getOfficeName();
				}else{
					officeId = userInfo.getParentId();
					RestServiceResult<SysOfficeEntity> rsysOfficeEntity = xypjRemoteBaseService.getOfficesById(officeId);
					SysOfficeEntity sysOfficeEntity = rsysOfficeEntity.getData();
					officeName = sysOfficeEntity.getOfficeName();
				}
				
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", officeId);
				map.put("name", officeName);
				list.add(map);
				
			}
			result.setData(list);
			result.setCode(RestServiceResult.CODE_SUCCESS);
			
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("系统异常");
		}
		return result;
	}
	
	

	@RequestMapping(value = "/getDengji.json")
	public RestServiceResult<List<Map<String, Object>>> getDengji(HttpServletRequest request,String id) {
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
		
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("id", "");
		map1.put("name", "全部");
		list.add(map1);
		
		if(id != null && !"".equals(id)){
			Map<String, Object> modelMap = szjgService.getModelByOffice(ywId,id);
			if(modelMap != null && modelMap.get("model_id") != null){
				List<String> rlist = szjgService.getDengjiByModel(String.valueOf(modelMap.get("model_id")));
				for (String s : rlist){
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("id", s);
					map.put("name", s);
					list.add(map);
				}
				
			}else{
				List<DictDetailEntity> rlist = sysDictService.getDictDetails("GJFRDJ", StringUtils.EMPTY);
				for(DictDetailEntity d:rlist){
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("id", d.getDtlValue());
					map.put("name", d.getDtlValue());
					list.add(map);
				}
			}
		}else{
			List<DictDetailEntity> rlist = sysDictService.getDictDetails("GJFRDJ", StringUtils.EMPTY);
			for(DictDetailEntity d:rlist){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", d.getDtlValue());
				map.put("name", d.getDtlValue());
				list.add(map);
			}
		}
		result.setData(list);
		result.setCode(RestServiceResult.CODE_SUCCESS);
		return result;
	}
	
	
	@RequestMapping(value = "/getXyjgDengji.json")
	public RestServiceResult<List<Map<String, Object>>> getXyjgDengji(HttpServletRequest request,String id) {
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
		
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		if(id != null && !"".equals(id)){
			Map<String, Object> modelMap = szjgService.getModelByOffice(ywId,id);
			if(modelMap != null && modelMap.get("model_id") != null){
				List<String> rlist = szjgService.getDengjiByModel(String.valueOf(modelMap.get("model_id")));
				for (String s : rlist){
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("id", s);
					map.put("name", s);
					list.add(map);
				}
				
			}else{
				List<DictDetailEntity> rlist = sysDictService.getDictDetails("GJFRDJ", StringUtils.EMPTY);
				for(DictDetailEntity d:rlist){
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("id", d.getDtlValue());
					map.put("name", d.getDtlValue());
					list.add(map);
				}
			}
		}else{
			List<DictDetailEntity> rlist = sysDictService.getDictDetails("GJFRDJ", StringUtils.EMPTY);
			for(DictDetailEntity d:rlist){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", d.getDtlValue());
				map.put("name", d.getDtlValue());
				list.add(map);
			}
		}
		result.setData(list);
		result.setCode(RestServiceResult.CODE_SUCCESS);
		return result;
	}
	
	@RequestMapping(value = "/getOptionalColums.json")
	public RestServiceResult<List<OptionalColumn>> getOptionalColums() {
		RestServiceResult<List<OptionalColumn>> r = new RestServiceResult<List<OptionalColumn>>();
		try {
			r = new RestServiceResult<List<OptionalColumn>>(ResourceSzjgXypjFieldsEnum.getOptionalColumns());
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常");
		}
		return r;
	}
	
	
	@RequestMapping(value = "/getDrqyTableList.json")
	public RestServiceResult<List<Map<String, Object>>> getDrqyTableList(
			@RequestParam(value = "name", defaultValue = "", required = true) String name,
			@RequestParam(value = "startTime", defaultValue = "", required = true) String startTime,
			@RequestParam(value = "endTime", defaultValue = "", required = true) String endTime,
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
			HttpServletRequest request) {

		String[] configColumns = (String[]) MapCache.get(1000 + "_" + "Drqy");
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}
		String[] additionalColumns = { "id","topDeptId" };
		List<String> showColumns = (configColumns == null || configColumns.length == 0)
				? ResourceSzjgDrqyFieldsEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);
		RestServiceResult<List<Map<String, Object>>> r = new RestServiceResult<List<Map<String, Object>>>();
		try {
			List<Map<String, Object>> tableList = null;
			int i  = userInfo.getUserType();
//			userType 1 普通用户应该只能查看自己的数据。 
//			userType 4 部门管理员能够查看本部门所有用户上报数据。 
//			userType 2 区域管理员能够查看本区域所有用户上报的数据。 
//			userType 5 数据管理员能够查询所有用户上报数据。 
//			userType 3 admin超级用户查看所有用户上报数据。
			
			if(i == 3){
				tableList = szjgService.getByDrqyConditionForPage(name,startTime,endTime,null,null,null,
						showColumns, additionalColumns,condition, page);
			}else if(i == 5){
				tableList = szjgService.getByDrqyConditionForPage(name,startTime,endTime,null,null,null,
						showColumns, additionalColumns,condition, page);
			}else if(i == 4){
				tableList = szjgService.getByDrqyConditionForPage(name,startTime,endTime,null,String.valueOf(userInfo.getOfficeId()),null,
						showColumns, additionalColumns,condition, page);
			}else if(i == 2){
				tableList = szjgService.getByDrqyConditionForPage(name,startTime,endTime,null,null,String.valueOf(userInfo.getAreaId()),
						showColumns, additionalColumns,condition, page);
			}else{
				tableList = szjgService.getByDrqyConditionForPage(name,startTime,endTime,String.valueOf(userInfo.getId()),null,null,
						showColumns, additionalColumns,condition, page);
			}
			
			r = new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常！");
		}
		return r;
	}
	
	@RequestMapping(value = "/getDrqyOptionalColums.json")
	public RestServiceResult<List<OptionalColumn>> getDrqyOptionalColums() {
		RestServiceResult<List<OptionalColumn>> r = new RestServiceResult<List<OptionalColumn>>();
		try {
			r = new RestServiceResult<List<OptionalColumn>>(ResourceSzjgDrqyFieldsEnum.getOptionalColumns());
		} catch (Exception e) {
			r.setCode(RestServiceResult.CODE_UNKNOW);
			r.setMsg("系统异常");
		}
		return r;
	}
	
	
	 @RequestMapping("/batchAdd.json")
	    public RestServiceResult<Object> batchAdd(@RequestParam(value = "file") CommonsMultipartFile file,
	    		@RequestParam(value = "name", defaultValue = "", required = true) String name,
	                                              HttpServletRequest request) {
		 	RestServiceResult<Object> res = new RestServiceResult<Object>();  
		 	if (file == null) {
	            res.setCode(-1);
                res.setMsg("上传文件不得为空！");

                ExceptionLogger.error("SzjgController#batchAdd", "上传文件不得为空！");
                return res;
	        }

	        
	        RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

	        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

	            SysUser userInfo = user.getData();

	            if (null != userInfo && userInfo.getId() != null) {

	               try {
					szjgService.addQy(file, String.valueOf(userInfo.getId()),name,
							   String.valueOf(userInfo.getOfficeId()),String.valueOf(userInfo.getAreaId()),
							   userInfo.getOfficeName(),userInfo.getAllParentId(),userInfo.getUsername());
				} catch (Exception e) {
					 res.setCode(-1);
			         res.setMsg("模板不匹配,导入文件失败！");

			         ExceptionLogger.error("SzjgController#batchAdd", "未获取到当前用户信息！");
			         return res;
				}

	                res.setCode(RestServiceResult.CODE_SUCCESS);

	            } else {

	                res.setCode(-1);
	                res.setMsg("未获取到当前用户信息！");

	                ExceptionLogger.error("SzjgController#batchAdd", "未获取到当前用户信息！");
	            }

	        } else {

	            res.setCode(-1);
	            res.setMsg("未获取到当前用户信息！");

	            ExceptionLogger.error("SzjgController#batchAdd", "未获取到当前用户信息！");
	        }

	        return res;
	    }
	 
	 @RequestMapping("/exportTemplate.json")
	    public void exportTemplate(HttpServletResponse response) throws IOException {

	        FileUtils.downloadFile(this.getClass().getResource("/信用监管.xlsx").getPath(), response);

	    }
	
	 
	 
	 
	 @RequestMapping(value = "/getDrqydfTableList.json")
		public RestServiceResult<List<Map<String, Object>>> getDrqydfTableList(
				@RequestParam(value = "id", defaultValue = "", required = true) String id,
				@RequestParam(value = "topDeptId", defaultValue = "", required = true) String topDeptId,
				@RequestParam(value = "rank", defaultValue = "", required = true) String ranks,
				@RequestParam(value = "startfen", defaultValue = "", required = true) String startfen,
				@RequestParam(value = "endfen", defaultValue = "", required = true) String endfen,				 
				HttpServletRequest request) {

//			String[] configColumns = (String[]) MapCache.get(1000 + "_" + "Drqydf");
			String token = HttpServletHelper.getUserToken(request);
			RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
			// 获取当前用户信息
			RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
			SysUser userInfo = null;
			if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
				userInfo = user.getData();
				if (null == userInfo || userInfo.getId() <= 0) {
					result.setCode(RestServiceResult.CODE_UNKNOW);
					result.setMsg("系统异常，请稍后再试！");
					ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
					return result;
				}
			} else {
				result.setCode(user.getCode());
				result.setMsg(user.getMsg());
				return result;
			}
//			String[] additionalColumns = { "drId","id" };
//			List<String> showColumns = (configColumns == null || configColumns.length == 0)
//					? ResourceSzjgDrqydfFieldsEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);
			RestServiceResult<List<Map<String, Object>>> r = new RestServiceResult<List<Map<String, Object>>>();
			try {
				List<Map<String, Object>> tableList = null;
				
				Map<String, Object> modelMap = szjgService.getModelByOffice(ywId,topDeptId);
				
				if(modelMap != null && modelMap.get("model_id") != null){
					tableList = szjgService.getByDrqydf(String.valueOf(modelMap.get("model_id")),id,ranks,startfen,endfen);
				}else{
					tableList = szjgService.get2ByDrqydf(id,ranks,startfen,endfen);
				}
				
				r = new RestServiceResult<List<Map<String, Object>>>(tableList);
			} catch (Exception e) {
				r.setCode(RestServiceResult.CODE_UNKNOW);
				r.setMsg("系统异常！");
			}
			return r;
		}
	 
	 @RequestMapping(value = "/getDrqydfOptionalColums.json")
		public RestServiceResult<List<OptionalColumn>> getDrqydfOptionalColums() {
			RestServiceResult<List<OptionalColumn>> r = new RestServiceResult<List<OptionalColumn>>();
			try {
				r = new RestServiceResult<List<OptionalColumn>>(ResourceSzjgDrqydfFieldsEnum.getOptionalColumns());
			} catch (Exception e) {
				r.setCode(RestServiceResult.CODE_UNKNOW);
				r.setMsg("系统异常");
			}
			return r;
		}

	 @RequestMapping("/addXyjg.json")
	  public RestServiceResult<Object> addXyjg(@RequestParam(value = "json", defaultValue = "", required = true) String json,
			  @RequestParam(value = "yongtu", defaultValue = "", required = true) String yongtu) {
		 RestServiceResult<Object> res = new RestServiceResult<Object>();
		 
		 if (json == null || "".equals(json)) {
	            throw new ServiceException("选择企业不得为空");
	        }
		 
		 try {
			 List<XyjgDrqydf> xyjgDrqydfList = JSONArray.parseArray(json, XyjgDrqydf.class);
			 	
			 Long id = szjgService.addXyjg(xyjgDrqydfList.get(0).getDrId(), yongtu);
			 for(XyjgDrqydf xyjgDrqydf: xyjgDrqydfList){
				 szjgService.addXyjgqy(xyjgDrqydf,String.valueOf(id));
			 }
			 
			} catch (Exception e) {
				res.setCode(RestServiceResult.CODE_UNKNOW);
				res.setMsg("系统异常");
			}
		 return res;
	 }
	    
	 
	 @RequestMapping(value = "/getXyjgOptionalColums.json")
		public RestServiceResult<List<OptionalColumn>> getXyjgOptionalColums() {
			RestServiceResult<List<OptionalColumn>> r = new RestServiceResult<List<OptionalColumn>>();
			try {
				r = new RestServiceResult<List<OptionalColumn>>(ResourceSzjgXyjgFieldsEnum.getOptionalColumns());
			} catch (Exception e) {
				r.setCode(RestServiceResult.CODE_UNKNOW);
				r.setMsg("系统异常");
			}
			return r;
		}
	 
	 @RequestMapping(value = "/getXyjgTableList.json")
		public RestServiceResult<List<Map<String, Object>>> getXyjgTableList(
				@RequestParam(value = "id", defaultValue = "", required = true) String id,
				@RequestParam(value = "name", defaultValue = "", required = true) String name,
				@RequestParam(value = "startTime", defaultValue = "", required = true) String startTime,
				@RequestParam(value = "endTime", defaultValue = "", required = true) String endTime,
				@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
				HttpServletRequest request) {

			String[] configColumns = (String[]) MapCache.get(1000 + "_" + "xyjg");
			String token = HttpServletHelper.getUserToken(request);
			RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
			// 获取当前用户信息
			RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
			SysUser userInfo = null;
			if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
				userInfo = user.getData();
				if (null == userInfo || userInfo.getId() <= 0) {
					result.setCode(RestServiceResult.CODE_UNKNOW);
					result.setMsg("系统异常，请稍后再试！");
					ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
					return result;
				}
			} else {
				result.setCode(user.getCode());
				result.setMsg(user.getMsg());
				return result;
			}
			String[] additionalColumns = {"id" };
			List<String> showColumns = (configColumns == null || configColumns.length == 0)
					? ResourceSzjgXyjgFieldsEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);
			RestServiceResult<List<Map<String, Object>>> r = new RestServiceResult<List<Map<String, Object>>>();
			try {
				List<Map<String, Object>> tableList = null;
				int i  = userInfo.getUserType();
//				userType 1 普通用户应该只能查看自己的数据。 
//				userType 4 部门管理员能够查看本部门所有用户上报数据。 
//				userType 2 区域管理员能够查看本区域所有用户上报的数据。 
//				userType 5 数据管理员能够查询所有用户上报数据。 
//				userType 3 admin超级用户查看所有用户上报数据。
				
				
				if(i == 3){
					tableList = szjgService.getByXyjgConditionForPage(id,name,startTime,endTime,null,null,null,
							showColumns, additionalColumns,condition, page);
				}else if(i == 5){
					tableList = szjgService.getByXyjgConditionForPage(id,name,startTime,endTime,null,null,null,
							showColumns, additionalColumns,condition, page);
				}else if(i == 4){
					tableList = szjgService.getByXyjgConditionForPage(id,name,startTime,endTime,null,String.valueOf(userInfo.getOfficeId()),null,
							showColumns, additionalColumns,condition, page);
				}else if(i == 2){
					tableList = szjgService.getByXyjgConditionForPage(id,name,startTime,endTime,null,null,String.valueOf(userInfo.getAreaId()),
							showColumns, additionalColumns,condition, page);
				}else{
					tableList = szjgService.getByXyjgConditionForPage(id,name,startTime,endTime,String.valueOf(userInfo.getId()),null,null,
							showColumns, additionalColumns,condition, page);
				}
				
				r = new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);
			} catch (Exception e) {
				r.setCode(RestServiceResult.CODE_UNKNOW);
				r.setMsg("系统异常！");
			}
			return r;
		}
	 
	 
	 @RequestMapping(value = "/getXyjgqyOptionalColums.json")
		public RestServiceResult<List<OptionalColumn>> getXyjgqyOptionalColums() {
			RestServiceResult<List<OptionalColumn>> r = new RestServiceResult<List<OptionalColumn>>();
			try {
				r = new RestServiceResult<List<OptionalColumn>>(ResourceSzjgXyjgqyFieldsEnum.getOptionalColumns());
			} catch (Exception e) {
				r.setCode(RestServiceResult.CODE_UNKNOW);
				r.setMsg("系统异常");
			}
			return r;
		}
	 
	 @RequestMapping(value = "/getXyjgqyTableList.json")
		public RestServiceResult<List<Map<String, Object>>> getXyjgqyTableList(
				@RequestParam(value = "id", defaultValue = "", required = true) String id,
				@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
				HttpServletRequest request) {

			String[] configColumns = (String[]) MapCache.get(1000 + "_" + "xyjgqy");
			String token = HttpServletHelper.getUserToken(request);
			RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
			// 获取当前用户信息
			RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
			SysUser userInfo = null;
			if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
				userInfo = user.getData();
				if (null == userInfo || userInfo.getId() <= 0) {
					result.setCode(RestServiceResult.CODE_UNKNOW);
					result.setMsg("系统异常，请稍后再试！");
					ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
					return result;
				}
			} else {
				result.setCode(user.getCode());
				result.setMsg(user.getMsg());
				return result;
			}
			String[] additionalColumns = null;
			List<String> showColumns = (configColumns == null || configColumns.length == 0)
					? ResourceSzjgXyjgqyFieldsEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);
			RestServiceResult<List<Map<String, Object>>> r = new RestServiceResult<List<Map<String, Object>>>();
			try {
				List<Map<String, Object>> tableList = null;
				tableList = szjgService.getByXyjgqyConditionForPage(id,showColumns, additionalColumns,condition, page);
				
				r = new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);
			} catch (Exception e) {
				r.setCode(RestServiceResult.CODE_UNKNOW);
				r.setMsg("系统异常！");
			}
			return r;
		}
	 
	 @RequestMapping(value = "/getXyjgqyAdvancedConditionColumns.json")
		public RestServiceResult<List<OptionalColumn>> getXyjgqyAdvancedConditionColumns() {
			return new RestServiceResult<List<OptionalColumn>>(ResourceSzjgXyjgqyFieldsEnum.getAdvancedConditionColumns());

		}
	 
	 @RequestMapping(value = "/addXyjgqyShowColunms.json")
		public RestServiceResult<String> addXyjgqyShowColunms(@RequestParam(value = "columns", required = true) String[] columns,
				HttpServletRequest request) {
			RestServiceResult<String> result = new RestServiceResult<String>();
			// 获取当前用户信息
			RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
			SysUser userInfo = null;
			if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
				userInfo = user.getData();
				if (null == userInfo || userInfo.getId() <= 0) {
					result.setCode(RestServiceResult.CODE_UNKNOW);
					result.setMsg("系统异常，请稍后再试！");
					ExceptionLogger.error("SzjgController#addShowColunms", "未获取到当前用户信息！");
					return result;
				}

				String column = StringUtils.join(columns, ",");
				//column = column + ",managerId" + ",areaName";
				MapCache.set(1000 + "_" + "xyjgqy", StringUtils.split(column, ","));
			} else {
				result.setCode(user.getCode());
				result.setMsg(user.getMsg());
				return result;
			}
			return result;
		}
	 
	 @RequestMapping(value = "/exportXyjgqy")
		public RestServiceResult<Object> exportXyjgqy(
				@RequestParam(value = "id", defaultValue = "", required = true) String id,
				@RequestParam(value = "columnEN", defaultValue = "", required = true) String columnEN,
				@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
				HttpServletResponse response, HttpServletRequest request) {
			String token = HttpServletHelper.getUserToken(request);
			RestServiceResult<Object> result = new RestServiceResult<Object>();
			// 获取当前用户信息
			RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
			SysUser userInfo = null;
			if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
				userInfo = user.getData();
				if (null == userInfo || userInfo.getId() <= 0) {
					result.setCode(RestServiceResult.CODE_UNKNOW);
					result.setMsg("系统异常，请稍后再试！");
					ExceptionLogger.error("BatchLogController#exportBookcontentList", "未获取到当前用户信息！");
					return result;
				}
			} else {
				result.setCode(user.getCode());
				result.setMsg(user.getMsg());
				return result;
			}
			// 导出结果限制1000条
			page.setLimit(1000);
			List<String> showColumns = JSONArray.parseArray(columnEN, String.class);

			List<Map<String, Object>> tableList;
			try {
				tableList = szjgService.getByXyjgqyConditionForPage(id,showColumns, null,condition, page);
				
				ExcelUtil.downloadExcel(response, tableList, "企业信用监管", "企业信用监管", showColumns,
						ResourceSzjgXyjgqyFieldsEnum.getOptionalColumns());
				return result;
			} catch (Exception e) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("批次处理信息导出异常，请稍后再试！");
				ExceptionLogger.error("批次处理信息导出异常！", e.getMessage(), e);
				return result;
			}
		}
	 
	 @RequestMapping(value = "/getDrqydfAdvancedConditionColumns.json")
		public RestServiceResult<List<OptionalColumn>> getDrqydfAdvancedConditionColumns() {
			return new RestServiceResult<List<OptionalColumn>>(ResourceSzjgDrqydfFieldsEnum.getAdvancedConditionColumns());

		}
	 
	 @RequestMapping(value = "/addDrqydfShowColunms.json")
		public RestServiceResult<String> addShowColunms(@RequestParam(value = "columns", required = true) String[] columns,
				HttpServletRequest request) {
			RestServiceResult<String> result = new RestServiceResult<String>();
			// 获取当前用户信息
			RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
			SysUser userInfo = null;
			if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
				userInfo = user.getData();
				if (null == userInfo || userInfo.getId() <= 0) {
					result.setCode(RestServiceResult.CODE_UNKNOW);
					result.setMsg("系统异常，请稍后再试！");
					ExceptionLogger.error("SzjgController#addShowColunms", "未获取到当前用户信息！");
					return result;
				}

				String column = StringUtils.join(columns, ",");
				//column = column + ",managerId" + ",areaName";
				MapCache.set(userInfo.getId() + "_" + "Drqydf", StringUtils.split(column, ","));
			} else {
				result.setCode(user.getCode());
				result.setMsg(user.getMsg());
				return result;
			}
			return result;
		}
	 
	 @RequestMapping(value = "/exportDrqydf")
		public RestServiceResult<Object> exportDrqydf(
				@RequestParam(value = "id", defaultValue = "", required = true) String id,
				@RequestParam(value = "type", defaultValue = "1", required = false) String type,
				@RequestParam(value = "topDeptId", defaultValue = "", required = true) String topDeptId,
				@RequestParam(value = "rank", defaultValue = "", required = true) String ranks,
				@RequestParam(value = "startfen", defaultValue = "", required = true) String startfen,
				@RequestParam(value = "endfen", defaultValue = "", required = true) String endfen,
				HttpServletResponse response) {
			RestServiceResult<Object> result = new RestServiceResult<Object>();
			
			// 导出结果限制1000条
			//page.setLimit(1000);
			//String columnEN = ["qymc","tyshxydm","score","rank"];
			
			List<String> showColumns = new ArrayList<String>();
			showColumns.add("qymc");
			showColumns.add("tyshxydm");
			showColumns.add("score");
			showColumns.add("rank");

			List<Map<String, Object>> tableList;
			try {
				
				Map<String, Object> modelMap = szjgService.getModelByOffice(ywId,topDeptId);
				if(modelMap != null && modelMap.get("model_id") != null){
					tableList = szjgService.getByDrqydf(String.valueOf(modelMap.get("model_id")),id,ranks,startfen,endfen);
				}else{
					tableList = szjgService.get2ByDrqydf(id,ranks,startfen,endfen);
				}
				if("1".equals(type)){
					ExcelUtil.downloadExcel(response, tableList, "企业信用评价_监管名单", "企业信用评价_监管名单", showColumns,
							ResourceSzjgDrqydfFieldsEnum.getOptionalColumns());
				}else{
					ExcelUtil.downloadExcel(response, tableList, "企业信用评价_重点关注名单", "企业信用评价_重点关注名单", showColumns,
							ResourceSzjgDrqydfFieldsEnum.getOptionalColumns());
				}
				
				return result;
			} catch (Exception e) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("批次处理信息导出异常，请稍后再试！");
				ExceptionLogger.error("批次处理信息导出异常！", e.getMessage(), e);
				return result;
			}
		}
	 
	 
	 
	 
	 @RequestMapping(value = "/getXyjgtjTableList.json")
		public RestServiceResult<List<Map<String, Object>>> getXyjgtjTableList(
				@RequestParam(value = "deptId", defaultValue = "", required = true) String deptId,
				@RequestParam(value = "yongtu", defaultValue = "", required = true) String yongtu,
				@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
				HttpServletRequest request) {

			String[] configColumns = (String[]) MapCache.get(1000 + "_" + "xyjgtj");
			String token = HttpServletHelper.getUserToken(request);
			RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
			// 获取当前用户信息
			RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
			SysUser userInfo = null;
			if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
				userInfo = user.getData();
				if (null == userInfo || userInfo.getId() <= 0) {
					result.setCode(RestServiceResult.CODE_UNKNOW);
					result.setMsg("系统异常，请稍后再试！");
					ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
					return result;
				}
			} else {
				result.setCode(user.getCode());
				result.setMsg(user.getMsg());
				return result;
			}
			String[] additionalColumns = { "id" };;
			List<String> showColumns = (configColumns == null || configColumns.length == 0)
					? ResourceSzjgXyjgtjFieldsEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);
			RestServiceResult<List<Map<String, Object>>> r = new RestServiceResult<List<Map<String, Object>>>();
			try {
				List<Map<String, Object>> tableList = null;
				tableList = szjgService.getByXyjgtjConditionForPage(deptId,yongtu,showColumns, additionalColumns,condition, page);
				
				r = new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);
			} catch (Exception e) {
				r.setCode(RestServiceResult.CODE_UNKNOW);
				r.setMsg("系统异常！");
			}
			return r;
		} 
	 
	 @RequestMapping(value = "/getXyjgtjOptionalColums.json")
		public RestServiceResult<List<OptionalColumn>> getXyjgtjOptionalColums() {
			RestServiceResult<List<OptionalColumn>> r = new RestServiceResult<List<OptionalColumn>>();
			try {
				r = new RestServiceResult<List<OptionalColumn>>(ResourceSzjgXyjgtjFieldsEnum.getOptionalColumns());
			} catch (Exception e) {
				r.setCode(RestServiceResult.CODE_UNKNOW);
				r.setMsg("系统异常");
			}
			return r;
		}
	 
	 @RequestMapping(value = "/getXyjgtjAdvancedConditionColumns.json")
		public RestServiceResult<List<OptionalColumn>> getXyjgtjAdvancedConditionColumns() {
			return new RestServiceResult<List<OptionalColumn>>(ResourceSzjgXyjgtjFieldsEnum.getAdvancedConditionColumns());

		}
	 
	 @RequestMapping(value = "/addXyjgtjShowColunms.json")
		public RestServiceResult<String> addXyjgtjShowColunms(@RequestParam(value = "columns", required = true) String[] columns,
				HttpServletRequest request) {
			RestServiceResult<String> result = new RestServiceResult<String>();
			// 获取当前用户信息
			RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
			SysUser userInfo = null;
			if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
				userInfo = user.getData();
				if (null == userInfo || userInfo.getId() <= 0) {
					result.setCode(RestServiceResult.CODE_UNKNOW);
					result.setMsg("系统异常，请稍后再试！");
					ExceptionLogger.error("SzjgController#addShowColunms", "未获取到当前用户信息！");
					return result;
				}

				String column = StringUtils.join(columns, ",");
				//column = column + ",managerId" + ",areaName";
				MapCache.set(1000 + "_" + "xyjgtj", StringUtils.split(column, ","));
			} else {
				result.setCode(user.getCode());
				result.setMsg(user.getMsg());
				return result;
			}
			return result;
		}
	 
	 @RequestMapping(value = "/exportXyjgtj")
		public RestServiceResult<Object> exportXyjgtj(
				@RequestParam(value = "deptId", defaultValue = "", required = true) String deptId,
				@RequestParam(value = "yongtu", defaultValue = "", required = true) String yongtu,
				@RequestParam(value = "columnEN", defaultValue = "", required = true) String columnEN,
				@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
				HttpServletResponse response, HttpServletRequest request) {
			String token = HttpServletHelper.getUserToken(request);
			RestServiceResult<Object> result = new RestServiceResult<Object>();
			// 获取当前用户信息
			RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
			SysUser userInfo = null;
			if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
				userInfo = user.getData();
				if (null == userInfo || userInfo.getId() <= 0) {
					result.setCode(RestServiceResult.CODE_UNKNOW);
					result.setMsg("系统异常，请稍后再试！");
					ExceptionLogger.error("BatchLogController#exportBookcontentList", "未获取到当前用户信息！");
					return result;
				}
			} else {
				result.setCode(user.getCode());
				result.setMsg(user.getMsg());
				return result;
			}
			// 导出结果限制1000条
			page.setLimit(1000);
			List<String> showColumns = JSONArray.parseArray(columnEN, String.class);

			List<Map<String, Object>> tableList;
			try {
				tableList = szjgService.getByXyjgtjConditionForPage(deptId,yongtu,showColumns, null,condition, page);
				
				ExcelUtil.downloadExcel(response, tableList, "信用监管统计", "信用监管统计", showColumns,
						ResourceSzjgXyjgtjFieldsEnum.getOptionalColumns());
				return result;
			} catch (Exception e) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("批次处理信息导出异常，请稍后再试！");
				ExceptionLogger.error("批次处理信息导出异常！", e.getMessage(), e);
				return result;
			}
		}
	 
	 @RequestMapping(value = "/guiji.json")
	    public RestServiceResult<List<Map>> getXyjgGuiji(@RequestParam(defaultValue = "", required = true, value = "ztmc") String ztmc,
	    		@RequestParam(defaultValue = "", required = true, value = "bm") String bm,
	    		@RequestParam(defaultValue = "", required = true, value = "type") String type,
	    		HttpServletRequest request) {
	    	
	    	List<Map> lhcjList = szjgService.getLhcj(ztmc,bm,type);
	    	
	    	return new RestServiceResult<>(lhcjList);
	    }
}
